package com.followmeandroid.GCM;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.followmeandroid.MainActivity;
import com.followmeandroid.R;
import com.followmeandroid.util.FollowMeSharedPreferences;
import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * Created by Adam on 11/29/2014.
 */
public class GcmIntentService extends IntentService {
    static final String TAG = "MainActivity - GCM";
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    private static Context context;

    public GcmIntentService() {
        super("824565051619");
    }
    private String type;

    /**
     * Receives an intent and processes it.
     * @param intent    The intent to be processed.
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);
        type = extras.getString("type");
        String msg = extras.getString("statustext");

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                    .equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                    .equals(messageType)) {
                sendNotification("Deleted messages on server: "
                        + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) {

                //TODO
                switch(type.charAt(0)){
                    case('n'):
                        sendNotification(msg);
                        break;
                    default:
                        break;
                }
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    /**
     * Handles a given context.
     * @param context_in    The context to be handled.
     */
    public static void setContext(Context context_in){
        context = context_in;
    }

    /**
     * Sends a notification.
     * @param msg   The message to be sent.
     */
    private void sendNotification(String msg) {
        Log.d(TAG, "Preparing to send notification...: " + msg);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        //TODO make enums of notifications to be sent.  The id can later be used to cancel the message (remove it).
        int notification_id = 1;
        switch(type.charAt(0)){
            case 'n':
                mBuilder.setSmallIcon(R.drawable.common_signin_btn_icon_dark)
                        .setContentTitle("FollowMe " + type)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                        .setContentText(msg);
                notification_id = 1;
                FollowMeSharedPreferences.reloadMember(context);
                break;
            default:
                break;
        }


        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify( notification_id, mBuilder.build());
        Log.d(TAG, "Notification sent successfully.");

        //For now, reload the entire member profile

    }
}